/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PermPane.java
 *
 * Created on Aug 30, 2009, 5:13:43 PM
 */
package org.dbdir.client.gui;

import javax.swing.JOptionPane;
import org.dbdir.Client;
import org.dbdir.core.helper.ACL;

/**
 *
 * @author thotheolh
 */
public class PermPane extends javax.swing.JPanel {

    Client client;
    MainFrame frame;
    String user;

    /** Creates new form PermPane
     * @param client
     * @param frame
     */
    public PermPane(Client client, MainFrame frame) {
        initComponents();
        this.client = client;
        this.frame = frame;
        this.setSize(304, 235);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bottomPane = new javax.swing.JPanel();
        leftPane = new javax.swing.JPanel();
        mainBtn = new javax.swing.JButton();
        memBtn = new javax.swing.JButton();
        rightPane = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        contentPane = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        viewPathCB = new javax.swing.JComboBox();
        editPathCB = new javax.swing.JComboBox();
        viewUsrCB = new javax.swing.JComboBox();
        editUsrCB = new javax.swing.JComboBox();
        viewPassCB = new javax.swing.JComboBox();
        editPassCB = new javax.swing.JComboBox();
        viewDTCB = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        usrLbl = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        bottomPane.setLayout(new java.awt.BorderLayout());

        leftPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        mainBtn.setText("Back To Main");
        mainBtn.setToolTipText("Back to Main Page");
        mainBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mainBtnActionPerformed(evt);
            }
        });
        leftPane.add(mainBtn);

        memBtn.setText("Back To Member");
        memBtn.setToolTipText("Back to Member");
        memBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                memBtnActionPerformed(evt);
            }
        });
        leftPane.add(memBtn);

        bottomPane.add(leftPane, java.awt.BorderLayout.WEST);

        rightPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        jButton1.setText("Change");
        jButton1.setToolTipText("Change User Permission");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        rightPane.add(jButton1);

        bottomPane.add(rightPane, java.awt.BorderLayout.EAST);

        add(bottomPane, java.awt.BorderLayout.SOUTH);

        jLabel1.setText("View");

        jLabel2.setText("Edit");

        jLabel3.setText("DB Path: ");

        jLabel4.setText("DB User: ");

        jLabel5.setText("DB Pass: ");

        jLabel6.setText("Datetime: ");

        viewPathCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "true", "false" }));

        editPathCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "true", "false" }));

        viewUsrCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "true", "false" }));

        editUsrCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "true", "false" }));

        viewPassCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "true", "false" }));

        editPassCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "true", "false" }));

        viewDTCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "true", "false" }));

        jLabel7.setText("User Permission: ");

        usrLbl.setFont(new java.awt.Font("DejaVu Sans", 1, 13));

        javax.swing.GroupLayout contentPaneLayout = new javax.swing.GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(viewUsrCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editUsrCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(viewPassCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editPassCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(viewDTCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(contentPaneLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(viewPathCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(16, 16, 16)))
                        .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(contentPaneLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(editPathCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(jLabel2))))
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(usrLbl)))
                .addContainerGap(73, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(usrLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(viewPathCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editPathCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(viewUsrCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editUsrCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(viewPassCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editPassCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(viewDTCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        add(contentPane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    public void init() {
        String str = client.getAccStr();
        String[] value1 = str.split("=");
        user = value1[1];
        if (str.isEmpty() || (!value1[0].equals("permusr"))) {
        } else {
            usrLbl.setText(user);
        }
        initData();
    }

    private void initData() {
        ACL acl = client.getUserACL(user);
        viewPathCB.setSelectedItem(client.getHandler().convertBoolToStr(acl.isShowDbPath()));
        editPathCB.setSelectedItem(client.getHandler().convertBoolToStr(acl.isEditDbPath()));
        viewUsrCB.setSelectedItem(client.getHandler().convertBoolToStr(acl.isShowDbUsername()));
        editUsrCB.setSelectedItem(client.getHandler().convertBoolToStr(acl.isEditDbUsername()));
        viewPassCB.setSelectedItem(client.getHandler().convertBoolToStr(acl.isShowDbPassword()));
        editPassCB.setSelectedItem(client.getHandler().convertBoolToStr(acl.isEditDbPassword()));
        viewDTCB.setSelectedItem(client.getHandler().convertBoolToStr(acl.isShowDateTimeReg()));
    }

    private void mainBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mainBtnActionPerformed
        // TODO add your handling code here:
        frame.getActionPane();
    }//GEN-LAST:event_mainBtnActionPerformed

    private void memBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memBtnActionPerformed
        // TODO add your handling code here:
        frame.getMemPane();
    }//GEN-LAST:event_memBtnActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (client.setUserAcl(new ACL(user,client.getCurrentDBProfile(),client.getHandler().convertToBool(this.viewPathCB.getSelectedItem().toString()),client.getHandler().convertToBool(this.viewUsrCB.getSelectedItem().toString()),client.getHandler().convertToBool(this.viewPassCB.getSelectedItem().toString()),client.getHandler().convertToBool(this.viewDTCB.getSelectedItem().toString()),client.getHandler().convertToBool(this.editPathCB.getSelectedItem().toString()),client.getHandler().convertToBool(this.editUsrCB.getSelectedItem().toString()),client.getHandler().convertToBool(this.editPassCB.getSelectedItem().toString())))) {
            JOptionPane.showMessageDialog(null, "Permission changed successfully.", "Permission Changed", JOptionPane.INFORMATION_MESSAGE);
            frame.getPermPane();
        } else {
            if (client.getAccStr().equals("err-modacl-deny-mod-admin")) {
                JOptionPane.showMessageDialog(null, "Permission FAILED to be changed.\nYou are not allowed to modify another admin's permissions.", "Permission Change Failed", JOptionPane.WARNING_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "Permission FAILED to be changed.", "Permission Change Failed", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPane;
    private javax.swing.JPanel contentPane;
    private javax.swing.JComboBox editPassCB;
    private javax.swing.JComboBox editPathCB;
    private javax.swing.JComboBox editUsrCB;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel leftPane;
    private javax.swing.JButton mainBtn;
    private javax.swing.JButton memBtn;
    private javax.swing.JPanel rightPane;
    private javax.swing.JLabel usrLbl;
    private javax.swing.JComboBox viewDTCB;
    private javax.swing.JComboBox viewPassCB;
    private javax.swing.JComboBox viewPathCB;
    private javax.swing.JComboBox viewUsrCB;
    // End of variables declaration//GEN-END:variables
}
